package conf

import (
	"embed"
	"gopkg.in/yaml.v2"
	"log"
	"os"
)

//用于解析yaml
type Base struct {
	Mysql             Mysql
	Redis             Redis
	Log               Log
}

var Yamls *Base
var EnvFile string
var env *Env

//go:embed yamls
var f embed.FS

func InitEnv() {
	env = new(Env)
	yamlFile, err := f.ReadFile("yamls/env.yaml")
	if err != nil {
		log.Fatalf("读取env.yaml读取错误: %v", err)
		return
	}
	err = yaml.Unmarshal(yamlFile, env)
	if err != nil {
		log.Fatalf("解析env conf.yaml读取错误: %v", err)
	}
	if name := os.Getenv("NAME"); name != "" {
		env.Name = name
	}

	EnvFile = env.Name + ".yaml"
}

//yaml 解析公共类
func InitYaml() {
	conf := new(Base)
	yamlFile, err := f.ReadFile("yamls/" + EnvFile)
	if err != nil {
		log.Fatalf("读取config.yaml读取错误: %v", err)
		return
	}

	err = yaml.Unmarshal(yamlFile, conf)
	if err != nil {
		log.Fatalf("解析setting conf.yaml读取错误: %v", err)
	}
	Yamls = conf

	return
}

func GetConf() *Base {
	return Yamls
}

func GetEnv() *Env {
	return env
}
